﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices.AccountManagement;

namespace Chisholm.Windows.Live.Security
{
    /// <summary>
    /// This contains extended properties available in Active Directory and useable to return
    /// Windows Live@EDU e-mail addresses
    /// </summary>
    [DirectoryObjectClass("user")]
    [DirectoryRdnPrefix("CN")]
    public class ExtendedUserPrincipal : UserPrincipal
    {
        /// <summary>
        /// Creates a new extended user principal based on given context
        /// </summary>
        /// <param name="context">The principal context the user exists in</param>
        public ExtendedUserPrincipal(PrincipalContext context)
            : base(context) { }

        /// <summary>
        /// Creates a new extended user principal from the given context, account name and password
        /// </summary>
        /// <param name="context">The principal context the user exists in</param>
        /// <param name="samAccountName">The account name of the user</param>
        /// <param name="password">The password of the user</param>
        /// <param name="enabled">Whether the user is enabled or not</param>
        public ExtendedUserPrincipal(PrincipalContext context, string samAccountName, string password,
            bool enabled)
            : base(context, samAccountName, password, enabled) { }    
    
        /// <summary>
        /// Returns the extended user principal from the given context matching the identity value
        /// </summary>
        /// <param name="context">The context the user exists in</param>
        /// <param name="identityvalue">The value of the user principal</param>
        /// <returns>An extended user principal matching the specified identity</returns>
        public static new ExtendedUserPrincipal FindByIdentity(PrincipalContext context, string identityvalue)
        {
            return (ExtendedUserPrincipal)UserPrincipal.FindByIdentityWithType(context, 
                typeof(ExtendedUserPrincipal), identityvalue);
        }

        /// <summary>
        /// This returns a value from the Active Directory which is specified separate to the "default"
        /// values on the user principal. It will null if the value attribute is not 
        /// found
        /// </summary>
        /// <typeparam name="valuetype">The type the value is returned in</typeparam>
        /// <param name="valuename">The name of the value in Active Directory to return</param>
        /// <returns>The value of the property specified or null if the value doesn't exist</returns>
        public valuetype GetExtendedValue<valuetype>(string valuename)
        {
            object[] result = this.ExtensionGet(valuename);
            if (result != null && result.Length > 0 && result[0].GetType() == typeof(valuetype))
            {
                // The result is of the value specified and exists, so cast & return it
                return (valuetype)result[0];
            }
            else if (result != null && result.Length > 0 && typeof(valuetype) == typeof(string))
            {
                // The result is a string so rather than casting return it's ToString() method
                object stringresult = result[0].ToString();
                return (valuetype)stringresult;
            }
            else
                // The result is not valid so don't return it
                return default(valuetype);
        }
    }
}
